﻿using System;
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Plugins;
using Xenta.Structs;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class InvoiceTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        protected override IDictionary<DependencyInfo, IDependency> Dependencies
        {
            get
            {
                var components = new Dictionary<DependencyInfo, IDependency>();
                var salesModel = new Mock<ISalesModel>();
                components.Add(new DependencyInfo
                {
                    ID = "52fa95dc-61b3-47b8-af7d-9453d8269d30"
                }, salesModel.Object);
                var shippingMethod = new Mock<IShippingMethod>();
                shippingMethod.Setup(x => x.CanShipFrom(It.IsAny<IAddress>()))
                    .Returns(true);
                shippingMethod.Setup(x => x.CanShipTo(It.IsAny<IAddress>()))
                    .Returns(true);
                components.Add(new DependencyInfo
                {
                    ID = "c616d39a-88bf-42b8-947a-d7a752abc176"
                }, shippingMethod.Object);
                return components;
            }
        }

        [TestCase(1, 1, 1)]
        public void Can_CRUD(decimal total, decimal paidAmount, decimal refundAmount)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;
            var customer = Processor.Execute(new CreateCustomer
            {
                AccountID = account.EntityID
            }).Result;
            var customerAddr = Processor.Execute(new CreateCustomerAddress
            {
                HolderID = customer.EntityID,
                CountryID = country.EntityID,
                RegionID = localizationProfile.Region.EntityID
            }).Result;
            var seller = Processor.Execute(new CreateSeller
            {
                AccountID = account.EntityID
            }).Result;
            var sellerAddr = Processor.Execute(new CreateSellerAddress
            {
                HolderID = seller.EntityID,
                CountryID = country.EntityID,
                RegionID = localizationProfile.Region.EntityID
            }).Result;
            var order = Processor.Execute(new CreateOrder
            {
                CustomerID = customer.EntityID,
                SellerID = seller.EntityID,
                BillingAddressID = customerAddr.EntityID,
                ShippingAddressID = customerAddr.EntityID,
                SellerAddressID = sellerAddr.EntityID,
                SalesModelID = Guid.Parse("52fa95dc-61b3-47b8-af7d-9453d8269d30"),
                ShippingMethodID = Guid.Parse("c616d39a-88bf-42b8-947a-d7a752abc176"),
                HandlingFee = Decimal.Zero,
                DiscountAmount = Decimal.Zero,
                ShippingPrice = Decimal.Zero,
                Subtotal = Decimal.Zero,
                TaxAmount = Decimal.Zero,
                Total = Decimal.Zero,
                Flags = OrderFlags.Pending
            }).Result;

            #endregion

            var dt = DateTime.UtcNow;
            var invoice = Processor.Execute(new CreateInvoice
            {
                OrderID = order.EntityID,
                Total = total,
                PaidAmount = paidAmount,
                RefundAmount = refundAmount,
                Flags = InvoiceFlags.Pending,
                IssuedOn = dt
            }).Result;

            Assert.NotNull(invoice);
            invoice.Total.AreEqual(total);
            invoice.PaidAmount.AreEqual(paidAmount);
            invoice.RefundAmount.AreEqual(refundAmount);
            invoice.IssuedOn.AreEqual(dt);
            invoice.Flags.AreEqual(InvoiceFlags.Pending);
            invoice.AreEqual(Processor.Execute(new GetInvoice
            {
                EntityID = invoice.EntityID
            }).Result);
            invoice.AreEqual(Processor.Execute(new GetInvoiceByGuid
            {
                Guid = invoice.Guid
            }).Result);

            total = total * 2;
            paidAmount = paidAmount * 2;
            refundAmount = refundAmount * 2;
            dt = dt.AddHours(1);
            var upd_invoice = Processor.Execute(new UpdateInvoice
            {
                EntityID = invoice.EntityID,
                Total = total,
                PaidAmount = paidAmount,
                RefundAmount = refundAmount,
                Flags = InvoiceFlags.Paid,
                IssuedOn = dt
            }).Result;

            Assert.NotNull(upd_invoice);
            upd_invoice.Total.AreEqual(total);
            upd_invoice.PaidAmount.AreEqual(paidAmount);
            upd_invoice.RefundAmount.AreEqual(refundAmount);
            upd_invoice.Flags.AreEqual(InvoiceFlags.Paid);
            upd_invoice.IssuedOn.AreEqual(dt);

            Processor.Execute(new DeleteInvoice
            {
                EntityID = invoice.EntityID
            });
        }

        [TestCase(0, 1, 1)]
        [TestCase(-1, 1, 1)]
        [TestCase(1, -1, 1)]
        [TestCase(1, 1, -1)]
        public void Cannot_CreateUpdate_If_Total_LtOrEq_PaidAmountOrRefundAmount_Lt_Zero(decimal total,
            decimal paidAmount,
            decimal refundAmount)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateInvoice
            {
                Total = total,
                PaidAmount = paidAmount,
                RefundAmount = refundAmount
            }));
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateInvoice
            {
                Total = total,
                PaidAmount = paidAmount,
                RefundAmount = refundAmount
            }));
        }
    }
}
